package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2021/2/28 8:34
 * @Description 标签业务逻辑类
 * @version: 1.0.0
 */
@Service
public class LabelService {
    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 根据id删除标签
     * @param id
     */
    public void deleteById(String id){
        labelDao.deleteById(id);
    }

    /**
     * 修改标签
     * @param label
     */
    public void update(Label label){
        labelDao.save(label);
    }

    /**
     * 新增标签
     * @param label
     */
    public void add(Label label) {
        String id = idWorker.nextId()+"";
        label.setId(id);
        labelDao.save(label);
    }

    /**
     * 根据id查询标签
     * @param id
     * @return
     */
    public Label findById(String id) {
        //jpa中用了java中的新特新，返回的是对象是optional，要想回去目标对象，还要get（）就能获取它的目标对象
        return labelDao.findById(id).get();
    }

    /**
     * 查询所有标签
     * @return
     */
    public List<Label> findAll(){
        return labelDao.findAll();
    }

    /**
     * 条件查询
     * @return
     */
    public List<Label> findSearch(Label label) {
        Specification specification = new Specification<Label>() {
            /**
             * @param root 跟对象 就是传进来的包含了查询条件的对象
             * @param criteriaQuery 封装关键字，比如group by ，order by等 基本上用不着
             * @param criteriaBuilder 用来把条件封装成对象的，返回值就是最后要的条件对象
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();

                if(label.getLabelname() != null && !"".equals(label.getLabelname())) {
                    Predicate labelname = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    list.add(labelname);
                }
                if(label.getState() != null && !"".equals(label.getState())) {
                    Predicate state = criteriaBuilder.equal(root.get("state").as(String.class), label.getState());
                    list.add(state);
                }
                if(label.getRecommend() != null && !"".equals(label.getRecommend())) {
                    Predicate recommend = criteriaBuilder.equal(root.get("recommend").as(String.class), label.getRecommend());
                    list.add(recommend);
                }
                Predicate[] predicates = new Predicate[list.size()];
                predicates = list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        };
        return labelDao.findAll(specification);
    }

    /**
     * 条件分页查询
     * @param label
     * @param page
     * @param size
     * @return
     */
    public Page findSearch(Label label, int page, int size) {
        Specification specification = new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List list = new ArrayList<Label>();
                if(label.getLabelname() != null && !"".equals(label.getLabelname())) {
                    Predicate labelname = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    list.add(labelname);
                }
                if(label.getState() != null && !"".equals(label.getState())) {
                    Predicate state = criteriaBuilder.equal(root.get("state").as(String.class), label.getState());
                    list.add(state);
                }
                if(label.getRecommend() != null && !"".equals(label.getRecommend())) {
                    Predicate recommend = criteriaBuilder.equal(root.get("recommend").as(String.class), label.getRecommend());
                    list.add(recommend);
                }
                Predicate[] predicates = new Predicate[list.size()];
                list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        };
        Pageable pageable = PageRequest.of(page-1, size);
        return labelDao.findAll(specification, pageable);
    }
}
